#include "birdsprite.h"
#include"globalgamemsger.h"
#include<QMovie>
#include<QPainter>
#include<QtMath>
#include<QGraphicsScene>
#include<QRandomGenerator>
#include<QDebug>
#include<QTimer>
void BirdSprite::waitingAdvance()
{
    if(this->scene()!=NULL)
        y=this->scene()->sceneRect().height()/2+10*sin(selfTick*5*3.14/180.0);
    vy=0;
    angle=0;
}
void BirdSprite::flyingAdvance()
{
    //lifeTick++;
    dx=5;
    vy+=g;
    y+=vy;
    dy=vy;
    angle=atan(dy/dx)*180/3.14;
//    angle=0;
    if(y<0)y=0;
    //int TgetScore=(scene()->sceneRect().width()-x);
    //if(lifeTick>=TgetScore)
    //{
    //    lifeTick%=TgetScore;
    //    emit globalGameMsger::m.addScore();
    //}
}

void BirdSprite::randomFlyingAdvance()
{
    dx=0.5;
    angle=0;
    lifeTick+=QRandomGenerator::global()->bounded(1,3);
    dy=0.5*cos(lifeTick/180.0)+0.2*sin(lifeTick/178.0+30)+0.3*cos(lifeTick/90.0+70);
    y+=dy;
    angle=atan(dy/dx)*180/3.14;
    if(y<0)y=0;
}
void BirdSprite::deadAdvance()
{
    if(this->scene()->sceneRect().bottom()-112<=y+16)
    {
        //y=this->scene()->sceneRect().bottom()-112-16;
        return;
    }
    angle=90;
    vy+=g;
    y+=vy;
    //lifeTick=0;
}
BirdSprite::BirdSprite(float x0,float y0,float g0):x(x0),y(y0),g(g0),vy(0),angle(0),dx(5),dy(0),selfTick(0)
{
    advanceMethod=&BirdSprite::waitingAdvance;
    q=new QMovie(":/ImgSrc/Images/bird.gif");
    q->start();
    connect(&globalGameMsger::m,&globalGameMsger::jump,this,&BirdSprite::jump);
    connect(&globalGameMsger::m,&globalGameMsger::gameOver,this,&BirdSprite::onGameOver);
    connect(&globalGameMsger::m,&globalGameMsger::gameBegin,this,&BirdSprite::onGameBegin);
    connect(&globalGameMsger::m,&globalGameMsger::gameRestart,this,&BirdSprite::onRestart);
    connect(&globalTicker,&QTimer::timeout,this,&BirdSprite::tickCnt);
    setZValue(1);
    lifeTick=0;
}

BirdSprite::~BirdSprite()
{
    delete q;
}
#define OFFSETX -20
#define OFFSETY -15
void BirdSprite::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
{
    painter->drawImage(OFFSETX,OFFSETY,q->currentImage());
}

QRectF BirdSprite::boundingRect() const
{
    return QRectF(OFFSETX,OFFSETY,41,31);
}
void BirdSprite::advance(int phase)
{
    (this->*advanceMethod)();
    setPos(x,y);
    setRotation(angle);
    if(phase==0)return;
    if(!collidingItems().empty()&&advanceMethod!=&BirdSprite::deadAdvance)
        emit globalGameMsger::m.gameOver();
}

QPainterPath BirdSprite::shape() const
{
    QPainterPath ret;
    ret.addEllipse(boundingRect());
    return ret;
}
void BirdSprite::jump(float intensity)
{
    //if(advanceMethod!=&BirdSprite::waitingAdvance)
    if(advanceMethod==&BirdSprite::flyingAdvance)
        vy=-intensity;
        //emit globalGameMsger::m.gameBegin();
}
void BirdSprite::onGameOver()
{
    advanceMethod=&BirdSprite::deadAdvance;
    disconnect(&globalGameMsger::m,&globalGameMsger::jump,this,&BirdSprite::jump);
    q->stop();
}

void BirdSprite::onRestart()
{
    advanceMethod=&BirdSprite::waitingAdvance;
    waitingAdvance();
    connect(&globalGameMsger::m,&globalGameMsger::jump,this,&BirdSprite::jump);
    q->start();
    selfTick=0;
}

void BirdSprite::onGameBegin(int gamemode)
{
    lifeTick=QRandomGenerator::global()->bounded(0,360);
    if(gamemode==1)
    advanceMethod=&BirdSprite::randomFlyingAdvance;
    else if(gamemode==2)
    advanceMethod=&BirdSprite::flyingAdvance;
}
void BirdSprite::tickCnt()
{
    selfTick++;
}
